import {
  Pagination
} from './pagination.js'

export default class ListDone {
  data;
  elem;
  str;
  router = 'getlist';
  start = 0;
  end = 12;
  num = 1;
  goods;
  constructor(elem, data) {

    this.data = data
    if (typeof elem === 'string') elem = document.querySelector(elem)
    this.elem = elem
    this.changeList()

  }

  changePage(res) {
    console.log(res);
    this.start = (res - 1) * 8
    this.end = res * 8
    this.str = ''
    this.str += `<ul class='item-list clear'>`
    console.log(this.start, this.end);
    this.str += this.data.reduce((v, t, i) => {

      if (!(i >= this.start && i < this.end)) return v
      v += `
      <div class="goodsdivs" id="${t.productSid}">
      <img src="${t.proPictDir}">
      <p>${t.brandName}</p>
      <span>${t.productName}</span>
      <i>￥${t.minSellingPrice}</i>
      </div>
      `
      return v
    }, '')
    this.str += `</ul>`
    this.elem.innerHTML = this.str
    window.scrollTo({
      top: 0
    })
    this.goods = document.querySelectorAll('.goodsdivs');
    console.log(this.goods);
    this.item = document.querySelectorAll('.item')
    this.goods.forEach(item => {
      item.addEventListener('click', e => this.localHandler(e))
    })
    this.item.forEach(t => t.addEventListener('click', e => this.itemHandler(e)))
  }
  localHandler(e) {
    location.href = './shoppingcar.html' + "?" + e.path[1].id;
  }
  changeList() {
    this.p = new Pagination('.jump', {
      pageInfo: {
        pagenum: 1,
        pagesize: 8,
        totalsize: this.data.length,
        totalpage: Math.ceil(this.data.length / 8)
      },
      textInfo: {
        prev: '上一页',
        next: '下一页'
      }
    }, (r) => this.changePage(r))
  }
}